global class LeadCovertToAccountUtil {
    
	/*
	 	将潜在客户转换成为 客户 及 联系人
	
	*/
    WebService static String convertToAccont(String lid){

    	Map<String,String> resMap  = new Map<String,String>();
    	Lead ld = null;
    	try{
    		 ld = [select Id,Lastname,Firstname ,Name ,MobilePhone,Phone,Email,Website,CustomerConverted__c,
    		 	LeadSource , CustomerType__c,Country__c,City__c,Company,MainProductsAndCooperativeBrands__c,
    		 	Established__c,Position__c,Address__c,Title ,Status , IsConverted,ConvertedDate,ConvertedAccountid,
    		 	ConvertedContactId
    		   from Lead where Id =:lid  ];
    	}catch(Exception e){
    		return JSON.serialize(setResultMap(false,'转换失败: '+ e.getMessage()));
    	}
    	if(ld.CustomerConverted__c == true ){
    		
    		return JSON.serialize(setResultMap(false,'已经转换,请不要重复转换!'));
    	}
    	//转换客户
    	Id DealerAbroad = Utils.getRecordTypeIdByDevNameAndObjName('DealerAbroad', 'Account');
    	Id PublicRelationshipAbroad = Utils.getRecordTypeIdByDevNameAndObjName('PublicRelationshipAbroad', 'Account');

    	Id TerminalCustomerAbroad = Utils.getRecordTypeIdByDevNameAndObjName('TerminalCustomerAbroad', 'Account');
    	
    	Account acc = new Account();
    	acc.AccountSource = ld.LeadSource;
    	acc.CustomerRecordTypeText__c = ld.CustomerType__c;
    	if(acc.CustomerRecordTypeText__c == 'DealerAbroad'			 ){
			acc.RecordTypeId = DealerAbroad;
		}else if( acc.CustomerRecordTypeText__c == 'PublicRelationshipAbroad'){
			acc.RecordTypeId = PublicRelationshipAbroad;
		}else if( acc.CustomerRecordTypeText__c == 'TerminalCustomerAbroad' ){
			acc.RecordTypeId = TerminalCustomerAbroad;
		}
		acc.Country__c = ld.Country__c;
		acc.CityEng__c = ld.City__c;
		acc.Name = ld.Company;
		acc.MainProductAndCooperationBrand_del__c = ld.MainProductsAndCooperativeBrands__c;
		acc.Established__c = ld.Established__c;
		acc.Address__c = ld.Address__c;
		acc.Website = ld.Website;
		acc.PotentialCustomers__c = ld.Id ;
		
		//转投联系人
		Id DealerAbroad1 = Utils.getRecordTypeIdByDevNameAndObjName('DealerAbroad', 'Contact');
    	Id PublicRelationshipAbroad1 = Utils.getRecordTypeIdByDevNameAndObjName('PublicRelationshipAbroad', 'Contact');

    	Id TerminalCustomerAbroad1 = Utils.getRecordTypeIdByDevNameAndObjName('TerminalCustomerAbroad', 'Contact');
		Contact ct = new Contact();
		ct.FirstName = ld.Firstname;
		ct.LastName = ld.LastName;
		ct.MobilePhone = ld.MobilePhone;
		ct.Phone = ld.Phone;
		ct.Title = ld.Title ; 
		if(acc.CustomerRecordTypeText__c == 'DealerAbroad'			 ){
			ct.RecordTypeId = DealerAbroad1;
		}else if( acc.CustomerRecordTypeText__c == 'PublicRelationshipAbroad'){
			ct.RecordTypeId = PublicRelationshipAbroad1;
		}else if( acc.CustomerRecordTypeText__c == 'TerminalCustomerAbroad' ){
			ct.RecordTypeId = TerminalCustomerAbroad1;
		}
		//ct.Name = ld.Name ; 

		SavePoint sp = Database.setSavepoint();
		try {
		    
		    insert acc ; 
		    ct.AccountId = acc.Id ; 
		    insert ct ; 

		    //ld.IsConverted = true;
		    //ld.Status = 'Working';
		    ld.CustomerConverted__c = true;
		   // ld.ConvertedAccountid = acc.Id ; 
		   // ld.ConvertedContactId = ct.Id ; 
		    update ld; 

    		return JSON.serialize(setResultMap(true,acc.Id));


		}catch (Exception e) {
		    Database.rollback(sp);
    	
    		return JSON.serialize(setResultMap(false,'转换失败: '+ e.getMessage()));
		}
		

    }

    private static Map<String,String> setResultMap(Boolean isSuc,String message){
    	
    	Map<String,String> resMap  = new Map<String,String>();
    	resMap.put('code', isSuc ? '1':'0');
    	resMap.put('msg', message);
    	return resMap;

    }

}